home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** File: Menu.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1992 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "App.h" /* Get the application includes/typedefs, etc. */
- #include "App.Common.h" /* Get the stuff in common with rez. */
- #include "App.protos.h" /* Get the prototypes for the application. */
-
- #ifndef __AppMenu__
- #include "App.Menu.h"
- #endif
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __TREEOBJ2__
- #include "TreeObj2.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern Boolean gQuitApplication;
- extern Boolean gHasAppleEvents;
- extern WindowPtr gToolWindow;
- extern WindowPtr gClipboardWindow;
-
- static Boolean DoAdjustFileMenu(WindowPtr window);
- static Boolean DoAdjustEditMenu(WindowPtr window);
- static Boolean DoAdjustArrangeMenu(WindowPtr window);
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* •• Called by DTS.Lib..framework. •• */
-
- /* Enable and disable menus based on the current state. The user can only
- ** select enabled menu items. We set up all the menu items before calling
- ** MenuSelect or MenuKey, since these are the only times that a menu item can
- ** be selected. Note that MenuSelect is also the only time the user will see
- ** menu items. This approach to deciding what enable/disable state a menu
- ** item has the advantage of concentrating all the decision-making in one
- ** place, as opposed to being spread throughout the application. Other
- ** application designs may take a different approach that is just as valid. */
-
- #pragma segment Menu
- void DoAdjustMenus(void)
- {
- WindowPtr window;
- Boolean redrawMenuBar;
-
- window = FrontWindow();
-
- redrawMenuBar = DoAdjustFileMenu(window);
- redrawMenuBar |= DoAdjustEditMenu(window);
- redrawMenuBar |= DoAdjustArrangeMenu(window);
-
- if (redrawMenuBar)
- DrawMenuBar();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* •• Called by DTS.Lib..framework. •• */
-
- /* This is called when an item is chosen from the menu bar (after calling
- ** MenuSelect or MenuKey). It performs the right operation for each command.
- ** It is good to have both the result of MenuSelect and MenuKey go to one
- ** routine like this to keep everything organized. */
-
- #pragma segment Menu
- void DoMenuCommand(long menuResult)
- {
- short menuID, menuItem, daRefNum, saveMode;
- Str255 str;
- FileRecHndl frHndl, newFrHndl;
- WindowPtr window;
- MenuHandle menu;
- Rect tearRect;
- OSErr err;
-
- if (window = FrontWindowOfType(kwIsDocument))
- frHndl = (FileRecHndl)GetWRefCon(window);
- /* frHndl is valid only if it is one of our windows. */
-
- menuID = HiWord(menuResult); /* Use macros for efficiency to get */
- menuItem = LoWord(menuResult); /* menu item number and menu number. */
-
- switch (menuID) {
-
- case mApple:
- switch (menuItem) {
- case iAbout: /* Bring up alert for About. */
- HCenteredAlert(rAboutAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- default: /* All non-About items in this menu are DAs. */
- GetItem(GetMHandle(mApple), menuItem, str);
- daRefNum = OpenDeskAcc(str);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case iNew:
- err = NewDocument(&frHndl, kDocFileType, true);
- if (!err) {
- err = DoNewWindow(frHndl, nil, FrontWindowOfType(kwIsDocument),
- (WindowPtr)-1);
- if (err)
- DisposeDocument(frHndl);
- }
- if (err)
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case iOpen:
- err = OpenDocument(&frHndl, nil, fsRdWrPerm);
- if (!err) {
- err = DoNewWindow(frHndl, nil, FrontWindowOfType(kwIsDocument),
- (WindowPtr)-1);
- if (err)
- DisposeDocument(frHndl);
- }
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case iClose:
- DisposeOneWindow(window, kClose);
- break;
- case iSave:
- case iSaveAs:
- saveMode = (menuItem == iSave) ? kSave : kSaveAs;
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- saveMode = kSaveAs;
- err = SaveDocument(frHndl, window, saveMode);
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case iDuplicate:
- err = DuplicateDocument(frHndl, &newFrHndl);
- if (!err) {
- err = DoNewWindow(newFrHndl, nil, FrontWindowOfType(kwIsDocument),
- (WindowPtr)-1);
- if (err)
- DisposeDocument(newFrHndl);
- }
- if (err)
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case iPageSetup:
- DoSetCursor(&qd.arrow);
- PresentStyleDialog(frHndl);
- break;
- case iPrint:
- DoSetCursor(&qd.arrow);
- err = noErr;
- if (!(*frHndl)->d.doc.fhInfo.printRecValid)
- err = PresentStyleDialog(frHndl);
- if (!err) {
- err = PrintDocument(frHndl, true, true);
- PrintDocument(nil, false, false);
- }
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case iQuit:
- gQuitApplication = DisposeAllWindows();
- break;
- }
- break;
-
- case mEdit: /* Call SystemEdit for DA editing & MultiFinder. */
- if (!IsDAWindow(window)) {
- if (menuItem == iClipboard) {
- ShowHide(gClipboardWindow, !((WindowPeek)gClipboardWindow)->visible);
- CleanSendInFront(gClipboardWindow, FrontWindowOfType(kwIsDocument));
- break;
- }
- switch ((*frHndl)->fileState.sfType) {
- case kDocFileType:
- switch (menuItem) {
- case iUndo:
- case iRedo:
- DoUndoTask((*frHndl)->d.doc.root, menuItem - iUndo, true);
- break;
- case iCut:
- case iCopy:
- case iPaste:
- DoClipboard(frHndl, menuItem);
- break;
- case iClear:
- DoDelete(frHndl);
- break;
- #if VH_VERSION
- case iViewHier:
- err = NewDocument(&frHndl, kViewHierFileType, false);
- if (!err) {
- err = DoNewWindow(frHndl, nil, FrontWindowOfType(kwIsDocument),
- (WindowPtr)-1);
- if (err)
- DisposeDocument(frHndl);
- }
- if (err)
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- #endif
- }
- break;
- #if VH_VERSION
- case kClipboardFileType:
- if (menuItem == iViewHier) {
- err = NewDocument(&frHndl, kViewHierFileType, false);
- if (!err) {
- err = DoNewWindow(frHndl, nil, FrontWindowOfType(kwIsDocument),
- (WindowPtr)-1);
- if (err)
- DisposeDocument(frHndl);
- }
- if (err)
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- }
- break;
- case kViewHierFileType:
- BeginContent(window);
- switch (menuItem) {
- case iUndo:
- CTEUndo();
- break;
- case iCut:
- case iCopy:
- case iPaste:
- case iClear:
- CTEClipboard(menuItem - iCut + 2);
- break;
- }
- EndContent(window);
- break;
- #endif
- }
- }
- else SystemEdit(menuItem - 1);
- break;
-
- case mArrange:
- switch (menuItem) {
- case iShowHideToolPalette:
- ToolPaletteDisplay();
- break;
- default:
- DoArrange(frHndl, menuItem);
- break;
- }
- break;
-
- case mToolPalette:
- menu = GetMHandle(mToolPalette);
- switch (menuItem) {
- case -1:
- if (gToolWindow) {
- GetItem(menu, iTearInfo, str);
- BlockMove(str + 1, &tearRect, sizeof(Rect));
- MoveWindow(gToolWindow, tearRect.left, tearRect.top, false);
- ShowHide(gToolWindow, true);
- BringToFront(gToolWindow);
- HiliteWindows();
- }
- break;
- case iArrowTool:
- case iRectTool:
- case iRRectTool:
- case iOvalTool:
- case iPieTool:
- SetPaletteTool(menuItem - 1, false);
- break;
- }
- }
-
- HiliteMenu(0); /* Unhighlight what MenuSelect (or MenuKey) hilited. */
- }
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* This function either enables or disables a menu item. */
-
- #pragma segment Menu
- void EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
- {
- if (enable)
- EnableItem(menu, item);
- else
- DisableItem(menu, item);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- static Boolean DoAdjustFileMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl;
- short i, enableItem;
-
- menu = GetMHandle(mFile);
-
- if (IsDAWindow(window)) {
- for (i = iNew; i < iQuit; ++i) DisableItem(menu, i);
- EnableItem(menu, iClose); /* Let DAs do a close from the menu. */
- return(false);
- }
-
- EnableItem(menu, iNew); /* Set these for the no-windows state. */
- EnableItem(menu, iOpen);
- if (MaxBlock() < 0x10000L) {
- if (CompactMem(0x10000L) < 0x10000L) {
- DisableItem(menu, iNew); /* Running low on RAM. */
- DisableItem(menu, iOpen);
- }
- }
- DisableItem(menu, iClose);
- DisableItem(menu, iSave);
- DisableItem(menu, iSaveAs);
- DisableItem(menu, iDuplicate);
- DisableItem(menu, iPageSetup);
- DisableItem(menu, iPrint);
- if (window = FrontWindowOfType(kwIsDocument)) {
- EnableItem(menu, iClose);
- frHndl = (FileRecHndl)GetWRefCon(window);
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- enableItem = GetWindowDirty(window);
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- enableItem = true;
- EnableOrDisableItem(menu, iSave, enableItem);
- EnableItem(menu, iSaveAs);
- EnableItem(menu, iDuplicate);
- }
- EnableItem(menu, iPageSetup);
- EnableItem(menu, iPrint);
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- static Boolean DoAdjustEditMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl, frClip;
- short i, undoDepth, numUndos;
- Boolean haveDocument, ignore;
- Str255 str;
- StringPtr cptr;
- static Str32 clipboardText[3];
-
- menu = GetMHandle(mEdit);
-
- if (IsDAWindow(window)) {
- EnableItem(menu, iUndo);
- DisableItem(menu, iRedo);
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iPaste);
- EnableItem(menu, iClear);
- DisableItem(menu, iClipboard);
- #if VH_VERSION
- DisableItem(menu, iViewHier);
- #endif
- return(false);
- }
-
- for (i = iUndo; i <= iClear; ++i) DisableItem(menu, i);
-
- haveDocument = (window = FrontWindowOfType(kwIsDocument)) ? true : false;
- frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
-
- #if VH_VERSION
- EnableOrDisableItem(menu, iViewHier, haveDocument);
- #endif
-
- EnableItem(menu, iClipboard);
- if (!clipboardText[0][0]) {
- GetItem(menu, iClipboard, str);
- p2c(str);
- for (i = 0; str[i]; ++i)
- if (str[i] == ',')
- str[i] = 0;
- for (cptr = str, i = 0; i < 3; ++i) {
- c2p((char *)cptr);
- pcpy(clipboardText[i], cptr);
- cptr += (cptr[0] + 1);
- }
- }
- if (gClipboardWindow) {
- i = (((WindowPeek)gClipboardWindow)->visible) ? 1 : 0;
- pcpy(str, clipboardText[i]);
- pcat(str, clipboardText[2]);
- SetItem(menu, iClipboard, str);
- EnableItem(menu, iClipboard);
- }
-
- if (haveDocument) {
- switch ((*frHndl)->fileState.sfType) {
- case kDocFileType:
- GetUndoInfo(frHndl, &undoDepth, &numUndos);
- if (undoDepth)
- EnableItem(menu, iUndo);
- if (undoDepth < numUndos)
- EnableItem(menu, iRedo);
- if (mDerefRoot((*frHndl)->d.doc.root)->numSelected) {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- }
- frClip = (FileRecHndl)GetWRefCon(gClipboardWindow);
- if ((*((*frClip)->d.doc.root))->numChildren)
- EnableItem(menu, iPaste);
- break;
- #if VH_VERSION
- case kViewHierFileType:
- CTEEditMenu(&ignore, mEdit, iUndo, iCut);
- break;
- #endif
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- static Boolean DoAdjustArrangeMenu(WindowPtr window)
- {
- MenuHandle menu;
- short i, numChildren, numSelected, cnum, flag;
- Boolean haveDocument, redrawMenuBar;
- FileRecHndl frHndl;
- TreeObjHndl root, chndl;
- Str255 str;
- StringPtr cptr;
- static Boolean arrangeMenuEnabled = true;
- static Str32 toolPaletteText[3];
-
- menu = GetMHandle(mArrange);
-
- if (IsDAWindow(window)) {
- for (i = iShowHideToolPalette; i <= iUngroup; ++i) DisableItem(menu, i);
- redrawMenuBar = arrangeMenuEnabled;
- arrangeMenuEnabled = false;
- return(redrawMenuBar);
- }
-
- haveDocument = (window = FrontWindowOfType(kwIsDocument)) ? true : false;
- frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
-
- EnableItem(menu, iShowHideToolPalette);
- if (!toolPaletteText[0][0]) {
- GetItem(menu, iShowHideToolPalette, str);
- p2c(str);
- for (i = 0; str[i]; ++i)
- if (str[i] == ',')
- str[i] = 0;
- for (cptr = str, i = 0; i < 3; ++i) {
- c2p((char *)cptr);
- pcpy(toolPaletteText[i], cptr);
- cptr += (cptr[0] + 1);
- }
- }
- if (gToolWindow) {
- i = (((WindowPeek)gToolWindow)->visible) ? 1 : 0;
- pcpy(str, toolPaletteText[i]);
- pcat(str, toolPaletteText[2]);
- SetItem(menu, iShowHideToolPalette, str);
- EnableItem(menu, iShowHideToolPalette);
- }
-
- for (i = iMoveForward; i <= iUngroup; ++i) {
- CheckItem(menu, i, false);
- DisableItem(menu, i);
- }
- if (haveDocument) {
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- root = (*frHndl)->d.doc.root;
- numChildren = (*root)->numChildren;
- numSelected = mDerefRoot(root)->numSelected;
- if (numSelected) {
- for (cnum = (*root)->numChildren; cnum;) {
- chndl = GetChildHndl(root, --cnum);
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- if ((*chndl)->type == GROUPOBJ)
- EnableItem(menu, iUngroup);
- }
- }
- if ((numSelected) && (numChildren > 1)) {
- if ((flag = numSelected) == 1) {
- chndl = GetChildHndl(root, 0);
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- --flag;
- }
- if (flag) {
- EnableItem(menu, iMoveForward);
- EnableItem(menu, iMoveToFront);
- }
- if ((flag = numSelected) == 1) {
- chndl = GetChildHndl(root, -1); /* Last child. */
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- --flag;
- }
- if (flag) {
- EnableItem(menu, iMoveBackward);
- EnableItem(menu, iMoveToBack);
- }
- }
- if (numSelected > 1)
- EnableItem(menu, iGroup);
- }
- }
-
- redrawMenuBar = !arrangeMenuEnabled;
- arrangeMenuEnabled = true;
- return(redrawMenuBar);
- }
-
-
-
-